package atguigu.boot.util;

import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;

/**
 * @program: springboot-01-helloworld
 * @ClassName getProperties
 * @description:
 * @author: zwq
 **/
@Component
public class CommonUtil {

    /**
     * 获取.properties文件中的内容
     * @param fileName
     * @return
     * @throws IOException
     */
    public List<Map<String,Object>> getProperties(String fileName) throws IOException {
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        HashMap<String, Object> map = new HashMap<>();
        InputStream in=null;
        Properties pps = new Properties();
        try {
            in = new BufferedInputStream(new FileInputStream(fileName));
            //1.加载.properties文件
            // 使用InputStreamReader()来读取，解决中文乱码
            pps.load(new InputStreamReader(in,"gb2312"));
            //2.得到配置文件的名字
            Enumeration<?> enumeration = pps.propertyNames();
            //3.遍历获取并存入map中
            while (enumeration.hasMoreElements()){
                String strKey = (String) enumeration.nextElement();
                String property = pps.getProperty(strKey);
                map.put(strKey,property);
            }
            list.add(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭流
            in.close();
        }
        return list;
    }

    public static void main(String[] args) {

    }
}
